import request from '@/utils/request'
import { mapGetters } from 'vuex'

export default {
  provide () {
    return {
      Page: this
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    getNextNode (params = {}, url = this.getNextNodeUrl) {
      params.areaCode = params.areaCode || this.user.districtNo
      return request({
        url,
        method: 'get',
        params
      })
    },
    delNode(params, url = this.delNodeUrl) {
      return request({
        url,
        method: 'get',
        params
      })
    },
    getNodeInfo (params, url = this.getNodeInfoUrl) {
      return new Promise(resolve => {
        request({
          url,
          method: 'get',
          params
        }).then(res => {
          const { data } = res
          if (data.parentId === 0) {
            delete data.parentId
          }
          resolve(res)
        })
      })
    },    
    addNodeInfo(data, url = this.addNodeInfoUrl) {
      delete data.id
      return request({
        url,
        method: 'post',
        data
      })
    },
    updateNodeInfo (data, url = this.updateNodeInfoUrl) {
      return request({
        url,
        method: 'post',
        data
      })
    }
  }
}
